#    Copyright 2008, 2009 Tri-County Electric Cooperative, St. Matthews, South Carolina
#    This file is part of COOPGIS.
#    COOPGIS is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#    COOPGIS is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#    You should have received a copy of the GNU General Public License
#    along with COOPGIS.  If not, see <http://www.gnu.org/licenses/>.
#************************BEGIN Add Include Files to Path************************
import sys
NeededPaths=['/opt/coopgis/djangoproject/include', '/opt/coopgis/djangoproject']
for path in NeededPaths:
    if(not path in sys.path):
        sys.path.append(path)
#************************END   Add Include Files to Path************************
import application, cis, command, compatibleunit, geography, oms, staker, symbology
import COOPGIS_Ajax, COOPGIS_GPS, COOPGIS_Misc, COOPGIS_ReportLab, COOPGIS_String, COOPGIS_Update
import os, csv, shutil, datetime, time, reportlab, djangoproject.settings
from django.contrib.gis.db import models
from django.contrib.gis.geos import *
from django.http import HttpResponse
from django.contrib import admin
import _mysql as mysql



          
#Temporarily placed here until problem with importing COOPGISViewsMisc is figured out...  
def StringDateTimeToDateTime(DateTimeString):
    """Attempts To Convert:
    'YYYY-MM-DD HH-MM-SS.SSS...' to a DateTime,
    returns None if it fails,
    for example: 
               '2008-09-26 15:05:43.222615'
    to:  datetime.datetime(2008, 9, 26, 15, 5, 43, 222615)"""
    if(not DateTimeString):
        return None
    Year=int(DateTimeString[:4])
    Month=int(DateTimeString[5:7])
    Day=int(DateTimeString[8:10])
    Hour=int(DateTimeString[11:13])
    Minute=int(DateTimeString[14:16])
    Second=int(DateTimeString[17:19])  
    PartialSeconds=int(DateTimeString[20:])
    return datetime.datetime(Year, Month, Day, Hour, Minute, Second, PartialSeconds)

class ConductorPlacement(models.Model):
    """ConductorPlacement includes possible configurations of how a conductor
    May be placed, 10X10 grid, with pole being position #4, top of pole #8 
    4,4 is the center.  0,0 is the lower left most position possible.
    Negative Positions and high numbers are acceptable (never would expect to be higher then 12 or so).
    All positions are when looking at lines away from the substation
    Position 500X500 is defined as undefined"""
    Name=models.CharField(max_length=250)
    Description=models.TextField()   #REQUIRED FIELD FOR CONDUCTOR PLACEMENT
    HorizontalLocation=models.IntegerField()
    VerticalLocation=models.IntegerField()
    class Meta:
        db_table=u'coopgisconductorplacement'
        app_label='djangoproject'
    def GetOrCreate(self, Name, Description, HorizontalLocation=500, VerticalLocation=500):
        try:
            New=ConductorPlacement.objects.get(Name=Name)
            return New
        except:
            pass
        New=ConductorPlacement()
        New.Name=Name
        New.Description=Description
        New.HorizontalLocation=HorizontalLocation
        New.VerticalLocation=VerticalLocation
        New.save()
        return New
